package com.ms.p2p.service.impl;

import com.ms.p2p.bean.PageBean;
import com.ms.p2p.dao.MemberDao;
import com.ms.p2p.entity.account.Account;
import com.ms.p2p.entity.account.YuEbao;
import com.ms.p2p.entity.member.Member;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

@Service
@Transactional(readOnly = true)
public class MemberServiceImpl {

    @Autowired
    private  RedPacketServiceImpl redPacketService;

    @Autowired
    private MemberDao memberDao;
    @Autowired

    private YuEbaoServiceImpl yuEbaoService;
    @Autowired
    private MemberAccountServiceImpl memberAccountService;


    @Transactional(readOnly = false, propagation = Propagation.REQUIRED)
    public int updateById(Member map) {

        return memberDao.update(map);

    }
    @Transactional(readOnly = false, propagation = Propagation.REQUIRED)
    public void updateMobilePhoneById(String mobilePhone, Integer id) {

        memberDao.updateMobile(mobilePhone, id);

    }

    /**
     * 插入一条数据（部分字段）并返回主键
     *
     * @return 主键
     */
    @Transactional(readOnly = false, propagation = Propagation.REQUIRED)
    public int insertAndReturnKey(Member user) {
        return memberDao.insert(user);
    }

    /**
     * 根据ID查询一条用户数据
     *
     * @param userId 用户ID
     * @return User对象
     */
    public Member findById(int userId) {
        return memberDao.findById(userId);

    }

    /**
     * 根据用户名查询一条用户数据
     *
     * @return User对象
     */
    public Member findByUserName(String userName) {
        return memberDao.findByUserName(userName);

    }

    /**
     * 根据手机号查询一条数据
     *
     * @param mobilePhone
     * @return User对象
     */
    public Member findByMobilePhone(String mobilePhone) {
        return memberDao.findByMobilePhone(mobilePhone);

    }

    /**
     * 根据email号查询一条数据
     *
     * @return User对象
     */
    public Member findByEmail(String email) {
        return memberDao.findByEmail(email);

    }

    /**
     * 根据用户名或手机号或Email查询一条数据
     *
     * @return User对象
     */
    public Member findByUserNameOrMobilePhoneOrEmail(String str) {
        return memberDao.findUser(str);

    }

    public List<Map<String, Object>> findPager(String userId, String userName, String kefuname,String email, String mobilePhone, String begin_date, String end_date, String ru_userId, String ru_userName, String ru_mobilePhone, PageBean page) {
        return memberDao.findPager(userId, userName, kefuname,email, mobilePhone, begin_date, end_date, ru_userId, ru_userName, ru_mobilePhone, page.getBegin(), page.getPageSize());
    }

    public List<Map<String, Object>> findAll(String userId, String userName, String kefuname,String email, String mobilePhone, String begin_date, String end_date, String ru_userId, String ru_userName, String ru_mobilePhone) {
        return memberDao.findAll(userId, userName, kefuname, email, mobilePhone, begin_date, end_date, ru_userId, ru_userName, ru_mobilePhone);
    }


    public int findCount(String userId, String userName, String kefuname,String email, String mobilePhone, String begin_date, String end_date, String ru_userId, String ru_userName, String ru_mobilePhone) {
        return memberDao.findCount(userId, userName, kefuname, email, mobilePhone, begin_date, end_date, ru_userId, ru_userName, ru_mobilePhone);
    }


    /**
     * @return int 返回类型
     * @Title: countAllUser
     * @Description: 累计客户数
     */
    @Cacheable(value = "Index", key = "'countAllUser'")
    public int countAllUser() {

        return memberDao.countAllUser();

    }

    @Cacheable(value = "Index", key = "'countAllUserTender'")
    public int countAllUserTender() {
        return memberDao.countAllUserTender();
    }


    @Transactional(readOnly = false, propagation = Propagation.REQUIRED)
    public int update(Member map) {

        return memberDao.update(map);
    }
    @Transactional(readOnly = false, propagation = Propagation.REQUIRED)
    public int updatePayPwd(String pwd, int id) {
        return memberDao.updatePayPassword(pwd, id);
    }
    /**
     * 查询出单个用户推荐的注册用户的个数
     *
     * @param recommend_id
     * @return
     */
    public int findMyRecommendCount(Integer recommend_id) {
        return memberDao.countByrecommendId(recommend_id);

    }
    public int findNotUserIdAndEmail(Integer id, String email) {
        return memberDao.findNotMyeamal(id, email);


    }
    /**
     * 更新客服
     */
    @Transactional(readOnly = false, propagation = Propagation.REQUIRED)
    public int updateKefuByid(Integer user_id, String kefu_id) {
        return memberDao.updateKefuByid(kefu_id, user_id);

    }

    @Transactional(readOnly = false, propagation = Propagation.REQUIRED)
    public Member register(Member user)  {
        //保存用户信息
        int i = insertAndReturnKey(user);
        // 开户
        Account account = new Account();
        account.setUser_id(user.getId());
        memberAccountService.openAccount(account);
        YuEbao yuEbaoImpl = new YuEbao();
        yuEbaoImpl.setUser_id(user.getId());
        yuEbaoService.insert(yuEbaoImpl);
        redPacketService.AwardRedPacket(user);
        return user;
    }
}
